# -*- coding: utf-8 -*-
# make-android-icons.py
# MakeAppIcon
#
# Created by Jai on 2018/8/30.

from PIL import Image
import sys,os


class FileInfo():

    def __init__(self, folder, width, height):
        self.folder = folder
        self.width = width
        self.height = height
        pass

    pass


mipmap_hdpi     = FileInfo("mipmap-hdpi", 72, 72)
mipmap_mdpi     = FileInfo("mipmap-mdpi", 48, 48)
mipmap_xhdpi    = FileInfo("mipmap-xhdpi", 96, 96)
mipmap_xxhdpi   = FileInfo("mipmap-xxhdpi", 144, 144)
mipmap_xxxhdpi  = FileInfo("mipmap-xxxhdpi", 192, 192)

files = [mipmap_hdpi,mipmap_mdpi,mipmap_xhdpi,mipmap_xxhdpi,mipmap_xxxhdpi]


def cover(path, local=''):
    readPath = path
    app_name = "ic_launcher.png"
    app_name_r = "ic_launcher_round.png"
    if len(local) == 0 :
        local = os.path.abspath('./')
    localPath = os.path.join(local, 'res')
    if not os.path.exists(localPath):
        print 'should create', localPath
        os.mkdir(localPath)
        pass
    if path[0] == '.':
        readPath = os.path.join(os.path.abspath('./'), path)
        pass
    print 'reading', readPath
    img = Image.open(readPath)
    width,height = img.size
    if width < 1536 or height < 1536:
        print '\033[33msource image resolution should be 1536x1536 will work well\033[0m'
    for each in files:
        print 'resizing',each.folder
        folder = os.path.join(localPath, each.folder)
        if not os.path.exists(folder):
            os.mkdir(folder)
            pass
        out = img.resize((each.width,each.height),Image.ANTIALIAS)
        alphaImage = out.convert('RGBA')
        blendImage = Image.new('RGBA', alphaImage.size, (255, 255, 255, 1))
        out = Image.blend(blendImage, alphaImage, 1.0)
        out.save(os.path.join(folder, app_name), "PNG")
        out.save(os.path.join(folder, app_name_r), "PNG")
        pass
    pass

if __name__ == '__main__':
    if len(sys.argv) == 2:
        cover(sys.argv[1])
    elif len(sys.argv) == 3:
        cover(sys.argv[1], sys.argv[2])
    else:
        print 'usage: make-android-icons "file path" [output path]'
    pass































































